Practical Skills for
Biology Research II

R packages

Niklas Mähler

2021-10-05

Learning goals

  • Can describe what an R package is
  • Know where to search for existing R packages
  • Install packages from different sources
  • See what packages are loaded in a session
  • Basic knowledge of namespaces

What is an R package

  • Source code that someone else wrote

How do we use them?

The library function loads the package and makes anything exported from the package directly available

library(ggplot2)
head(luv_colours)
##          L             u         v           col
## 1 9341.570 -3.370649e-12    0.0000         white
## 2 9100.962 -4.749170e+02 -635.3502     aliceblue
## 3 8809.518  1.008865e+03 1668.0042  antiquewhite
## 4 8935.225  1.065698e+03 1674.5948 antiquewhite1
## 5 8452.499  1.014911e+03 1609.5923 antiquewhite2
## 6 7498.378  9.029892e+02 1401.7026 antiquewhite3

Unpack the box and put all the items on the table

Another way of using them

head(ggplot2::luv_colours)
##          L             u         v           col
## 1 9341.570 -3.370649e-12    0.0000         white
## 2 9100.962 -4.749170e+02 -635.3502     aliceblue
## 3 8809.518  1.008865e+03 1668.0042  antiquewhite
## 4 8935.225  1.065698e+03 1674.5948 antiquewhite1
## 5 8452.499  1.014911e+03 1609.5923 antiquewhite2
## 6 7498.378  9.029892e+02 1401.7026 antiquewhite3

Only take a specific item out of the box, use it, then put it back

Where can they be found?

  • CRAN – offical R repository
  • Bioconductor – mainly bioinformatics
  • Github

An R package could really be uploaded anywhere, but at least CRAN and Bioconductor don’t let anything through.

Installing packages from CRAN

# Install from CRAN
install.packages("ggplot2")

Installing packages from Bioconductor

# Install the BiocManager from CRAN 
install.packages("BiocManager")
# Install package from Bioconductor
BiocManager::install("qvalue")

Installing from Github

# We need the devtools package to install from Github
install.packages("devtools")
# Install the development version of ggplot2
devtools::install_github("tidyverse/ggplot2")

Order of operations

  • Installing a package is only required once, with a few exceptions
    • The R installation is updated
    • You want to use features in a package that has been updated
  • library needs to be called for every new session

Conflicting names

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:ggplot2':
## 
##     vars
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Conflicting names

The function that was imported last takes precedence. To access other functions, use <package>::<function>.

intersect
## function (x, y, ...) 
## UseMethod("intersect")
## <bytecode: 0x7fb9bcf4ee70>
## <environment: namespace:generics>
base::intersect
## function (x, y) 
## {
##     y <- as.vector(y)
##     unique(y[match(as.vector(x), y, 0L)])
## }
## <bytecode: 0x7fb9b96bb6f0>
## <environment: namespace:base>

Conflicting names

The conflicted package can help us avoid mistakes

library(conflicted)
library(dplyr)

filter
## Error: [conflicted] `filter` found in 2 packages.
## Either pick the one you want with `::` 
## * dplyr::filter
## * stats::filter
## Or declare a preference with `conflict_prefer()`
## * conflict_prefer("filter", "dplyr")
## * conflict_prefer("filter", "stats")

Conflicting names

We can then resolve the conflict for the rest of our session

conflict_prefer("filter", "dplyr")
## [conflicted] Will prefer dplyr::filter over any other package
filter
## function (.data, ..., .preserve = FALSE) 
## {
##     UseMethod("filter")
## }
## <bytecode: 0x7f9c99ae9f58>
## <environment: namespace:dplyr>

Packages I use (almost) daily

  • here
  • conflicted
  • dplyr
  • ggplot2
  • purrr
  • stringr
  • readr

You can write your own R packages!

RStudio is an excellent tool for developing R packages, and helps out with a lot of the formalities

Not a part of this course though, but check out the book R Packages if you are interested

R Packages cover